<?
	include("includes/config.php");
	include("includes/login.php");
	include("includes/functions.php");
	if(@$_GET['redeschide']){
			$nr_comanda = $_GET['nr_comanda'];
			mysql_query("update comenzi set status_comanda = 'deschisa' where numar_comanda = '$nr_comanda' ");
	}
	
	if(@$_POST['finalieaza_comanda']){
		$nr_comanda = $_GET['nr_comanda'];
		mysql_query("update comenzi set status_comanda = 'deviz' where numar_comanda = '$nr_comanda' ");
		header("location:/devize.php");
	}

	
?>
<html>
	<head>
		<link rel="stylesheet" type="text/css" href="css/style.css" />
	</head>
	<body>
		<div id="header"><h1> <? echo detaliFirma('nume_firma'); ?></h1></div>
		<div id="warper">
			<?
				$resMeniu = mysql_query("select * from meniu");
			?>
			<div id="menu">
				<ul>
					<? while($rowMen = mysql_fetch_array($resMeniu)): ?>
					<li><a href="<?echo $rowMen['link'];?>"><? echo $rowMen['numeLink']; ?></a></li>
					<? endwhile; ?>
				</ul>
			</div>
			<?
				$id_client= $_GET['id_client'];
				$id_masina = $_GET['id_masina'];
				$resdatecomada = mysql_query("select * from parteneri,parteneri_masini where  id_client = '$id_client' and id_masina = '$id_masina' ");
				$rowCmd = mysql_fetch_array($resdatecomada);
				
				$nume = $rowCmd['nume'];
				$telefon = $rowCmd['telefon'];
				$email = $rowCmd['email'];
				$nr_inm = $rowCmd['numar_inmatriculare'];
				$tipmasina = $rowCmd['tip_masina'];
				$adresa = $rowCmd['adresa'];
				
			
					
				
				if(@$_GET['nr_comanda']){
					$nr_comanda = $_GET['nr_comanda'];
				}else{
					$resSerie = mysql_query("select id_comanda, numar_comanda from comenzi order by id_comanda desc limit 1");
					if(mysql_num_rows($resSerie) >=1){
						$rowSerie = mysql_fetch_array($resSerie);
						$nr_comanda = $rowSerie['numar_comanda'];
						$nr_comanda = $nr_comanda + 1;
					}else{
						$nr_comanda = 1;
					}
					
				
				}
				$res_cmd = mysql_query("select numar_comanda,id_comanda from comenzi where numar_comanda = '$nr_comanda' ");
				if(mysql_num_rows($res_cmd)< 1){
					mysql_query("insert into comenzi (id_comanda,numar_comanda,id_client,id_masina,data_comanda,nume_persoana,telefon,email,numar_inmatriculare,tip_masina,adresa,status_comanda)  values(NULL,'$nr_comanda','$id_client','$id_masina',NOW(),'$nume','$telefon','$email','$nr_inm','$tipmasina','$adresa','deschisa')") or die(mysql_error());
					$id_comanda = mysql_insert_id();
				}else{
					$rows = mysql_fetch_array($res_cmd);
					$id_comanda = $rows['id_comanda'];
				
				}
				
				
			?>
			<div id="content">
				<div id="contentWrap">
					<div class="block" style="height:60px;">
						<div style="width:380px; float:left;height:60px;">
							<div style="width:380px;height:40px;" >
								<b style="color:#00a652;font-size:18px;">Comanda Deschisa Nr. <? echo detaliFirma('serie_comanda') . $nr_comanda;?></b>
							</div>
							
							<div style="width:385px;float:left">
								<a class="buton" href="/comandanoua.php?nr_comanda=<? echo $nr_comanda; ?>&id_client=1&id_masina=1" >Sumar </a>&nbsp;
								<a class="buton" href="/comandanoua_simptome_constatari.php?id_comanda=<? echo $id_comanda; ?>&nr_comanda=<? echo $nr_comanda; ?>&id_client=1&id_masina=1&nr_inmat=<? echo $nr_inm; ?>" >Simptome / Constatari </a>&nbsp;
								<a class="buton" href="/comandanoua_materiale.php?id_comanda=<? echo $id_comanda; ?>&nr_comanda=<? echo $nr_comanda; ?>&id_client=1&id_masina=1&nr_inmat=<? echo $nr_inm; ?>" >Materiale </a>&nbsp;
								<a class="buton"  href="/comandanoua_reparatii.php?id_comanda=<? echo $id_comanda; ?>&nr_comanda=<? echo $nr_comanda; ?>&id_client=1&id_masina=1&nr_inmat=<? echo $nr_inm; ?>"  >Reparatii </a>&nbsp;
							</div>
						</div>
						<div style="width:200px;height:55px;background:#707070;float:left;margin-left:180px;padding:4px;">
							<?
								/**************************** MATERIALE QUERY **************************/
									$res_pret_materiale = mysql_query("select pret,cant from comenzi_materiale,comenzi_materiale_relatii where comenzi_materiale.id_material = comenzi_materiale_relatii.id_material and id_comanda = '$id_comanda' ");
									$pret_materiale = array();
									while($rowPret = mysql_fetch_array($res_pret_materiale)){
										$pret_materiale[] = $rowPret['pret'] * $rowPret['cant']  ;
									}
									$pret_materiale = array_sum($pret_materiale);
								/**************************** MATERIALE QUERY **************************/	
									
								/**************************** REPARATI QUERY **************************/
									$res_pret_reparatii = mysql_query("select tarif,cant from comenzi_reparatii where  id_comanda = '$id_comanda' ");
									$pret_reparatii = array();
									while($rowPret = mysql_fetch_array($res_pret_reparatii)){
										$pret_reparatii[] = $rowPret['tarif'] * $rowPret['cant']  ;
									}
									$pret_reparatii = array_sum($pret_reparatii);
								/**************************** REPARATI QUERY **************************/
							?>
							Total materiale cu TVA: <? echo $pret_materiale; ?> RON<br/>
							
							Total Reparatii cu TVA <? echo $pret_reparatii; ?> RON<br/>
							<hr>
							<b>Total <? echo $pret_materiale + $pret_reparatii  ?> RON</b>
						</div>
					</div>
					
					<form action="" method="post">
						<div class="block">
							<div style="width:300px;height:250px; float:left;">
								<h4> Date client si autovehicol </h4>
								<p>
									<label>Nume Client</label>
									<input  class="text" type="text" nume="numeclient" value="<? echo $rowCmd['nume'];?>" />
								</p>
								<p>
									<label>Numar inmatriculare</label>
									<input  class="text" type="text" nume="auto" value="<? echo $rowCmd['numar_inmatriculare'];?>" />
								</p>
								<p>
									<label>Model</label>
									<input  class="text" type="text" nume="model" value="<? echo $rowCmd['tip_masina'];?>" />
								</p>
								<p>
									<label>Adresa</label>
									<input  class="text" type="text" nume="adresa" value="<? echo $rowCmd['adresa'];?>" />
								</p>
							</div>
						
							<div style="width:300px;height:250px; float:left;margin-left:80px;">
								<h4> Date Generale </h4>
								<p>
									<label>Nr. Kilometri Rulati</label>
									<input  class="text" type="text" nume="nr_km" value="<? echo $rowCmd['nr_km'];?>" />
								</p>
								<p>
									<label>Data Reparatiei</label>
									<input  class="text" type="text" nume="data" value="<? echo date("d-m-y") ?>" />
								</p>
								<p>
									<label>Termen de executie <small>(zile)</small></label>
									<input  class="text" type="text" nume="termen" value="" />
								</p>
								<p>
									<label>Responsabil verificare finala</label>
									<input  class="text" type="text" nume="responsabil" value="" />
								</p>
							</div>
							<div style="clear:both;"></div>
						</div>
						
						<div class="block" style="margin-bottom:20px;">
							<div style="width:300px;height:250px; float:left;">
								<h4> Date persoana de contact</h4>
								<p>
									<label>Nume persoana</label>
									<input  class="text" type="text" nume="nume" value="<? echo $rowCmd['nume'];?>" />
								</p>
								<p>
									<label>Nr. telefon</label>
									<input  class="text" type="text" nume="telefon" value="<? echo $rowCmd['telefon'];?>" />
								</p>
								<p>
									<label>Email</label>
									<input  class="text" type="text" nume="email" value="<? echo $rowCmd['email'];?>" />
								</p>
								<p>
									<label>Observatii</label>
									<input  class="text" type="text" nume="obs" value="" />
								</p>
							</div>
						
							<div style="width:300px;height:250px; float:left;margin-left:80px;">
								<h4> Asigurare</h4>
								<p>
									<label>Asigurator</label>
									<input  class="text" type="text" nume="asigurator" value="" />
								</p>
								<p>
									<label>Numar dosar dauna</label>
									<input  class="text" type="text" nume="nr_dosar" value="" />
								</p>
							</div>
							<div style="clear:both;"></div>
						</div>
								</form>
						
						
							<form action="" method="post">
								<input class="buton" type="submit" name="finalieaza_comanda" value="Finalizeaza comada / deschide deviz">
							</form>
						
								<input class="buton" type="submit" name="sterge" value="Sterge">&nbsp;
								<a  target="_BLANK" class="buton" href="/documente/comanda.php?deviz=true&id_comanda=<? echo $id_comanda; ?>">Listare Comanda</a>

					
			
					
				</div>
			</div>
		</div>
	</body>
</html>